<style>

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.form-item {
  margin-left: 10px;
  text-align: center;
  vertical-align: middle;
  margin-bottom: 5px;
  margin-top: 5px;
}

.topbutton {
  margin-top: 5px;
}
</style>

<template>

  <div>
    <!-- <el-row>
      <el-button plain>新增</el-button>
    </el-row> -->
    <el-form :inline="true">
      <el-form-item label="用户账号" class="form-item">
        <el-input v-model="searchMap.username" placeholder=""/>
      </el-form-item>
      <!-- <el-form-item label="活动时间" class="form-item">
        <el-col>
          <el-date-picker v-model="searchMap.startdate" type="date" placeholder="选择日期" style="width: 100%;" />
        </el-col>
      </el-form-item> -->
      <el-button type="primary" class="topbutton" @click="fetchData()">查询</el-button>
      <el-button type="primary" @click="handleEdit('')">新增</el-button>
    </el-form>
    <el-table
      :data="list"
      border
      style="width: 100%"
    >
      <el-table-column
        fixed
        prop="name"
        label="用户账号"
        width="150"
        align="center"
      />
      <el-table-column
        prop="password"
        label="密码"
        min-width="100"
        align="center"
      />
      <el-table-column
        prop="description"
        label="描述"
        width="120"
        align="center"
      />
      <el-table-column
        prop="createTime"
        label="注册日期"
        width="150"
        align="center"
      />
      <el-table-column
        prop="updateTime"
        label="更新日期"
        width="150"
        align="center"
      />
      <el-table-column
        prop="lastLoginTime"
        label="登录日期"
        width="150"
        align="center"
      />
      <!-- <el-table-column
        prop="img"
        label="头像"
        width="300"
        align="center"
      >
        <template slot-scope="scope">
          <img width="100%" height="200" :src="scope.row.img">
        </template>
      </el-table-column> -->
      <el-table-column
        prop="phone"
        label="电话"
        width="150"
        align="center"
      />
      <el-table-column
        label="操作"
        min-width="200"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <!-- <el-button type="text" size="small" @click="handleClick(scope.row)">查看</el-button> -->
          <el-button type="text" size="small" @click="handleEdit(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total, sizes, prev, pager, next"
      :total="total"
      :page-sizes="[10,20,50]"
      :page-size="10"
      @size-change="sizeData"
      @current-change="pageData"
    />
    <el-dialog title="新增/编辑" :visible.sync="dialogFormVisible">
      <el-form label-width="80px">
        <el-row>
          <el-col :span="11">
            <el-form-item label="用户账号">
              <el-input v-model="pojo.userName"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="密码">
              <el-input v-model="pojo.password"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="账号描述">
              <el-input v-model="pojo.description"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="注册日期">
              <el-input v-model="pojo.createTime"/>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-upload
          class="avatar-uploader"
          action="http://127.0.0.1:8080/phm/file/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :headers="headers"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload> -->
        <!-- <el-row>
          <el-col :span="11">
            <el-form-item label="电话"><el-input v-model="pojo.phone" /></el-form-item>
          </el-col>
        </el-row> -->

        <el-button type="primary" @click="handleSave()">保存</el-button>
        <el-button @click="dialogFormVisible = false">关闭</el-button>
      </el-form>

    </el-dialog>
  </div>
</template>

<script>
import {page, findById, deleteByIds, update} from '@/api/sys/user'

export default {

  data() {
    return {
      list: [],
      total: 0,
      searchMap: {
        curPage: 1,
        pageSize: 10
      },
      dialogFormVisible: false,
      id: '', // 主键，新增修改的主键
      pojo: {},
      imageUrl: '',
      // headers: {
      //   token: getToken() // 给图片赋值token
      // }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      page(this.searchMap).then(res => {
        console.log(res)
        this.list = res.data.records
        this.total = res.data.total
      })
    },
    dataFormat(row, column) {
      const date = row[column.property]
      if (date === undefined) {
        return ''
      }
      // return moment(date).format('YYYY-MM-DD')引用moment格式化日期
    },
    sizeData(limit) {
      console.log(limit)
      this.searchMap.pageSize = limit
      this.fetchData()
    },
    pageData(page) {
      this.searchMap.curPage = page
      this.fetchData()
    },
    // 新增和修改用同一个函数，新增不传 ID ,修改传ID过来
    handleEdit(id) {
      console.log(id)
      this.dialogFormVisible = true
      if (id !== '') {
        findById(id).then(res => {
          console.log(res)
          this.pojo = res.data
          this.id = id
          this.imageUrl = this.pojo.img
        })
      } else {
        this.id = ''
        this.pojo = {}
        this.imageUrl = ''
      }
    },
    handleDelete(id) {
      this.$confirm('此操作删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var ids = []
        ids.push(id)
        console.log(ids)
        deleteByIds(ids).then(res => {
          this.$message({
            type: (res.code === 20000 ? 'success' : 'error'),
            message: res.msg
          })
          if (res.code === 20000) {
            this.fetchData()
          }
        })
        // 向服务器发请求
        // this.$message({
        //   type: 'success',
        //   message: '删除成功!'
        // })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 保存数据的方法
    handleSave() {
      // 向后端发送 数据
      this.pojo.img = this.imageUrl
      update(this.id, this.pojo).then(res => {
        // 修改 或者新增成功
        this.$message({
          message: res.msg,
          type: (res.code === 20000 ? 'success' : 'error')
        })
        if (res.code === 20000) {
          this.fetchData()
        }
        this.dialogFormVisible = false
      })
    },
    handleAvatarSuccess(res, file) {
      console.log(res)
      this.imageUrl = 'http://127.0.0.1:8080' + res.data
      // console.log(file.raw)
      // this.imageUrl = URL.createObjectURL(file.raw)
      // 这句话绑定的是node返回的图片给你，应该发挥服务器给的图片地址
      // 保存提交时候还需要pojo.image=this.imageUrl这样就可以吧图片保存到服务器数据库里面
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>
